import {Pipe, PipeTransform} from '@angular/core';


@Pipe({
    name: 'date'
})


export class DatePipe implements PipeTransform {
    year: string;
    month: string;
    day: string;
    hours: string;
    minutes: string;
    seconds: string;
    result: string;

    transform(val: any, key: number) {
        if (!val) {
            return;
        }

        val = val.toString();

        if (val.length < 13) {
            val = parseInt(val + '000');
        } else {
            val = parseInt(val);
        }

        let date = new Date(val);
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hours = date.getHours();
        let minutes = date.getMinutes();
        let seconds = date.getSeconds();

        this.year = year.toString();

        if (month < 10) {
            this.month = '0' + month;
        } else {
            this.month = month.toString();
        }

        if (day < 10) {
            this.day = '0' + day;
        } else {
            this.day = day.toString();
        }

        if (hours < 10) {
            this.hours = '0' + hours;
        } else {
            this.hours = hours.toString();
        }

        if (minutes < 10) {
            this.minutes = '0' + minutes;
        } else {
            this.minutes = minutes.toString();
        }

        if (seconds < 10) {
            this.seconds = '0' + seconds;
        } else {
            this.seconds = seconds.toString();
        }


        switch (key) {
            case 1:
                this.result = this.year + '-' + this.month + '-' + this.day + ' ' + this.hours + ':' + this.minutes + ':' + this.seconds;
                break;

            case 2:
                this.result = this.year + '-' + this.month + '-' + this.day + ' ' + this.hours + ':' + this.minutes;
                break;

            case 3:
                this.result = this.year + '-' + this.month + '-' + this.day;
                break;

            case 4:
                this.result = this.month + '-' + this.day;
                break;

            case 5:
                this.result = this.hours + ':' + this.minutes;
                break;
        }

        return this.result;
    }

}